package com.artfii.fluentsql.core.tools;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeFormatterBuilder;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.Locale;
import java.util.logging.Logger;


public class JdkDateUtils {
    private static Logger logger = Logger.getLogger(DateUtils.class.getName());
    private static final ZoneId ZONE = ZoneId.systemDefault();

    private static DateTimeFormatter dtf = new DateTimeFormatterBuilder()
            .parseCaseInsensitive()
            .parseDefaulting(ChronoField.CLOCK_HOUR_OF_AMPM, 0)
            .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
            /*         .append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"))
                     .append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"))
                     .append(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSZ"))
                     .append(DateTimeFormatter.ofPattern("yyyyMMdd"))
                     .append(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                     .append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"))
                     .append(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"))
                     .append(DateTimeFormatter.ofPattern("yyyy/MM/dd"))
                     .append(DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss"))
                     .append(DateTimeFormatter.ofPattern("yyyy/MM/dd'T'HH:mm:ss"))
                     .append(DateTimeFormatter.ofPattern("yyyy/MM/dd'T'HH:mm:ss.SSS"))
                     .append(DateTimeFormatter.ofPattern("yyyy/MM/dd'T'HH:mm:ss.SSSZ"))
                     .append(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
                     .append(DateTimeFormatter.ofPattern("yyyyMMddHHmmssZ"))
                     .append(DateTimeFormatter.ofPattern("EEE, d MMM yyyy HH:mm:ss Z"))
                     .append(DateTimeFormatter.ofPattern("MMM d, yyyy HH:mm:ss a"))
                     .append(DateTimeFormatter.ofPattern("EEE, MMM d, ''yy"))*/
            .append(DateTimeFormatter.ofPattern("[MM/dd/uuuu hh:mm:ss a][MM/dd/uuuu HH[:mm[:ss]]][uuuuMMdd[ hh:mm:ss a]"))
            .toFormatter(Locale.ENGLISH);


    /**
     * 把常见的日期字串转日期
     *
     * @param dateStr
     * @return
     * @author:lqf
     */
    public static Date toDate(String dateStr) {
        if (StringKit.isEmpty(dateStr)) return null;
        if ("0000-00-00 00:00:00".equals(dateStr)) return null;

        try {
            Instant instant = LocalDateTime.parse(dateStr, dtf).atZone(ZONE).toInstant();
            return Date.from(instant);
        } catch (IllegalArgumentException e) {
            if (isPhpTime(dateStr)) {
                return new Date(Long.valueOf(dateStr).longValue());
            }
        }
        logger.info("日期解析出错:" + dateStr);
        return null;
    }
    private static boolean isPhpTime(String dataStr) {
        return StringKit.isNumber(dataStr) && 10 <= dataStr.length();
    }

    public static void main(String[] args) {
        String p = ""
                + "(yyyyMMdd[hh:mm:ss])"
                + "[yyyyMMdd][yyyy-MM-dd hh:mm:ss][MM-dd-yyyy hh:mm:ss a][MM-dd-yyyy HH[:mm[:ss]]][yyyyMMdd[ hh:mm:ss a]"
                + "(yyyy-MM-dd['T']HH[:]mm[:]ss[,S][.S]X)"
                ;


        DateTimeFormatter dtf = new DateTimeFormatterBuilder()
                .parseCaseInsensitive()
                .appendPattern(p)
                .parseDefaulting(ChronoField.MONTH_OF_YEAR, 1)
                .parseDefaulting(ChronoField.DAY_OF_MONTH, 1)
                .parseDefaulting(ChronoField.HOUR_OF_DAY, 0)
                .parseDefaulting(ChronoField.MINUTE_OF_HOUR, 0)
                .parseDefaulting(ChronoField.SECOND_OF_MINUTE, 0)
                .parseDefaulting(ChronoField.MILLI_OF_SECOND, 0)
                .toFormatter(Locale.ENGLISH);

        String[] arr = { "08/25/2021 10:20:30 am", "08/25/2021 10:20:30", "08/25/2021 10:20", "20210825",
                "2016-03-23 22:00:00","2016/03/23 22:00:00.256145","2016-03-23 22:00:00.123","23Mar2016:22:00:00.123"
                ,"23Mar2016:22:00:00.123 -0800"


        };

        for (String s : arr) {
            s = s.replaceAll("/","-");
            LocalDateTime ldt = LocalDateTime.parse(s, dtf);
            System.out.println(ldt);
        }

    }
}
